//
//  HKMeViewController.m
//  AQBPolice
//
//  Created by apple on 2017/7/5.
//  Copyright © 2017年 eight. All rights reserved.
//

#import "HKMeViewController.h"
#import "HKLoginViewController.h"

@interface HKMeViewController ()<UIAlertViewDelegate, UIActionSheetDelegate>

/**
 电话号码标签
 */
@property (weak, nonatomic) IBOutlet UILabel *phoneNumberLabel;

/**
 警号标签
 */
@property (weak, nonatomic) IBOutlet UILabel *policeNumberLabel;

/**
 警官名字
 */
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;

@end

@implementation HKMeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //读取手机号展示在界面上
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *string = [userDefaults objectForKey:@"getPhoneNumber"];
    
    NSString *preString = [string substringToIndex:3];
    NSString *sufString = [string substringFromIndex:7];
    
    self.phoneNumberLabel.text = [NSString stringWithFormat:@"%@****%@", preString, sufString];
    self.view.backgroundColor = HKColor(100, 107, 140);
    HKLog(@"警号是%@", policeNumber);
    //警号
    self.policeNumberLabel.text = [NSString stringWithFormat:@"%@", policeNumber];
    //警官名字
    self.nameLabel.text = [NSString stringWithFormat:@"%@警官", policeName];
}

#pragma mark - actions
//点击退出登录
- (IBAction)logoutButtonClicked:(id)sender {
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"确定要注销吗" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [sheet showInView:self.view];
}

#pragma mark - UIActionSheet delegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
        [self modifyPoliceState:@"1"];
        
        //清空手机号
        NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
        [defs removeObjectForKey:@"getPhoneNumber"];
        
        //回到登录界面
        HKLoginViewController *login = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"HKLoginViewController"];
        
        self.view.window.rootViewController = login;
        [MBProgressHUD showSuccess:@"已注销" toView:self.view.window];
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //修改状态栏的颜色为白色
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
}


/**
 修改出警的状态
 @param state 1为停警，2为出警
 */
- (void)modifyPoliceState:(NSString *)state {
    NSURLSession *session = [NSURLSession sharedSession];
    NSString *urlString = [baseURL stringByAppendingString:@"mobile/police/modifyPoliceState/"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
    request.HTTPMethod = @"POST";
    NSString *phoneStr = [NSString stringWithFormat:@"state=%@", state];//1表示停警，2表示出警
    NSString *msgStr = [NSString stringWithFormat:@"&policeId=%@", policeNumber];
    request.HTTPBody=[[phoneStr stringByAppendingString:msgStr] dataUsingEncoding:NSUTF8StringEncoding];
    NSURLSessionDataTask *task=[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSLog(@"%@",[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]);
        if (error) {//如果出现网络问题直接返回
            return;
        }
        if (data == nil) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [MBProgressHUD showError:@"网络错误" toView:self.view];
            });
        }else {
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
            if ([dict[@"meta"] isEqual: @"success"]) {//成功
                if([state isEqualToString:@"2"]) {
                    HKLog(@"修改状态为出警");
                }else {
                    HKLog(@"修改状态为停警");
                }
            }else {//失败
                if([state isEqualToString:@"2"]) {
                    HKLog(@"修改状态为停警");
                }else {
                    HKLog(@"修改状态为出警");
                }
            }
        }
    }];
    [task resume];
}

@end
